package com.isunland.managesystem.ui;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.webkit.WebView;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.isunland.managesystem.R;
import com.isunland.managesystem.base.BaseFragment;
import com.isunland.managesystem.common.ApiConst;
import com.isunland.managesystem.entity.Rwjmessagepublic;
import com.isunland.managesystem.utils.MyUtils;

public class CommonMessageDetailFragment extends BaseFragment {
  public static final String EXTRA_DETAIL =
      CommonMessageDetailFragment.class.getSimpleName() + ".EXTRA_DETAIL";

  private String mFileUrl;// 文件地址
  private String mFileName;

  private String mContentDetail;// 内容
  private Rwjmessagepublic detail;

  @Override public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // 设置返回图标
    setBack(true);
    setTitleCustom(R.string.messageDetail);
    detail = (Rwjmessagepublic) getArguments().getSerializable(EXTRA_DETAIL);
    if (detail != null) {
      mContentDetail = detail.getMessageContent();
      mFileUrl = detail.getFilepath1();
      mFileName = detail.getFileOriginalName();
    }
  }

  /**
   * 把标题和数据显示到页面
   */
  @SuppressLint("SetTextI18n") @Override public View onCreateView(LayoutInflater inflater,
      ViewGroup container, Bundle savedInstanceState) {

    inflater = LayoutInflater.from(getActivity());
    View view = inflater.inflate(R.layout.fragment_commonmessage_detail, container, false);
    TextView mTvMessageTtitle = (TextView) view.findViewById(R.id.tv_messageTtitle);
    TextView mTextView = (TextView) view.findViewById(R.id.tv_file_latestAnnouncement);
    WebView mWebView = (WebView) view.findViewById(R.id.wv_detail_latestAnnouncement);
    LinearLayout mllFileAnnounce = (LinearLayout) view.findViewById(R.id.ll_fileAnnounce);

    mTvMessageTtitle.setText(detail.getMessageHeader());
    //图文混排
    String content = MyUtils.parseWebImage(mContentDetail);//图片自适应
    mWebView.loadDataWithBaseURL(ApiConst.getUrlMain(), content, "text/html", "utf-8", null);//图文混排

    if (!TextUtils.isEmpty(mFileName)) {
      if ("(null)".equalsIgnoreCase(mFileName)) {
        mTextView.setText("");
      } else {
        mTextView.setText(mFileName);
      }
      mTextView.setOnClickListener(new OnClickListener() {
        @Override public void onClick(View view) {
          ExtraDownLoadDialogFragment dialog =
              ExtraDownLoadDialogFragment.newInstance("", mFileUrl);
          FragmentManager fm = getActivity().getSupportFragmentManager();
          dialog.show(fm, "");
        }
      });
    } else {
      mllFileAnnounce.setVisibility(View.GONE);
    }

    return view;
  }

  @Override // 响应返回图标
  public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {

      case android.R.id.home:
        getActivity().finish();
        return true;
      default:
        return super.onOptionsItemSelected(item);
    }
  }

  /**
   * 把数据上一个Fragment传递过来的数据存储到bundle中
   */
  public static Fragment newInstance(Rwjmessagepublic detail) {
    Bundle bundle = new Bundle();
    bundle.putSerializable(EXTRA_DETAIL, detail);
    CommonMessageDetailFragment lamdf = new CommonMessageDetailFragment();
    lamdf.setArguments(bundle);
    return lamdf;
  }
}
